﻿package controllers.read;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.User;

import models.Esexcerpt;
import models.Esgridcfg;
import models.Estate2;
import models.Estate2gd;
import models.EstateQuery;
import models.Proofrinfo;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;
import utils.mySqlUtil;

public class EsinfoController extends ApplicationController {
	
	public void index() throws Exception{
		User user = (User) rbac.getCurrentUser();
		List<Esgridcfg> fields = Esgridcfg.findAll(Esgridcfg.class," uid = '" + user.getLogin() + "' and isshow = 1 ",null,"sort");
		request.setAttribute("fields", fields);	
		request.setAttribute("fieldscount", fields.size());	
		
	}
	
	public void showmap(int qy) throws Exception{
		request.setAttribute("qyno", qy);	
	} 
	
	public View postCancelproofr(String id) throws Exception {
		User user = (User) rbac.getCurrentUser();
		String cond = "PF_local_id = ? and z_id = '" + user.getDepartment().getCompany_id() +"'";
		List<Proofrinfo> items = Proofrinfo.findAll(Proofrinfo.class, cond, null);
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除校对信息，内容为：" + new JsonView(items));									
		
		Proofrinfo.destroyAll(Proofrinfo.class, cond, new Object[]{id});
			
		return new JsonView("success:true");
	} 
	
	public View postProofr(String id,String content) throws Exception {
		User user = (User) rbac.getCurrentUser();
		Estate2 estate2 = Estate2.findFirst(Estate2.class,"local_id = ?",new Object[]{id});
		if (estate2 != null)
		{
			Proofrinfo item = new Proofrinfo();
			item.setContext(content);
			item.setEditdate(new java.sql.Timestamp(System
					.currentTimeMillis()));
			item.setInputdate(new java.sql.Timestamp(System
					.currentTimeMillis()));
			item.setOper(user.getLogin());
			item.setPf_local_id(id);
			item.setPf_unitname(estate2.getUnitname());
			item.setZ_id(user.getDepartment().getCompany_id());
			item.setUnitname(user.getDepartment().getSxname());
			item.save();
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")提交校对信息，内容为：" + new JsonView(item));									
			
		}
		
		return new JsonView("success:true");
	} 
	
	public void showEstateInfo(int id,String local_id) throws Exception {
		User user = (User) rbac.getCurrentUser();	
		Estate2 item = null;
		if ((local_id != null) && (!local_id.equals("")))
		{
			item = Estate2.findFirst(Estate2.class,"local_id = ? and flag = '0' and (" +  //必须是正常状态的记录
				"display = 0 or display = 1 "+  //无限制 和  只在中介的
"or (z_id in (select Company_ID from ZJList where Company_ID in ("+
" select mainid from ZJList where Company_ID = ?"+  //同个总公司的
"))) or z_id = ?)",new Object[]{local_id,user.getDepartment().getCompany_id(),user.getDepartment().getCompany_id()});
		}
        if (item == null){
        	item = Estate2.findFirst(Estate2.class,"id = ? and flag = '0' and (" +  //必须是正常状态的记录
    				"display = 0 or display = 1 "+  //无限制 和  只在中介的
    "or (z_id in (select Company_ID from ZJList where Company_ID in ("+
    " select mainid from ZJList where Company_ID = ?"+  //同个总公司的
    "))) or z_id = ?)",new Object[]{id,user.getDepartment().getCompany_id(),user.getDepartment().getCompany_id()});
        }
		
        request.setAttribute("item", item);	
	}
	
	public View getEstate2Items(EstateQuery estateQuery,int page, int rows, String q,int t, String sort, String order) throws Exception {
		if (page == 0) page = 1;
		if (rows == 0) rows = 50;
		User user = (User) rbac.getCurrentUser();		
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = mySqlUtil.getEstateQueryString(estateQuery,user.getDepartment().getCompany_id());
		cond += " and flag = '0' and (" +  //必须是正常状态的记录
				"display = 0 or display = 1 "+  //无限制 和  只在中介的
"or (z_id in (select Company_ID from ZJList where Company_ID in ("+
" select mainid from ZJList where Company_ID = ?"+  //同个总公司的
"))) or z_id = ?)";  //自己的
		List<Object> tmpArgs = new ArrayList<Object>();
		tmpArgs.add(user.getDepartment().getCompany_id());
		tmpArgs.add(user.getDepartment().getCompany_id());

		if (t != 0){
			Esexcerpt esexcerpt = Esexcerpt.find(Esexcerpt.class, t);
			
			if ((esexcerpt != null) && (esexcerpt.getContent()!=null) && (!esexcerpt.getContent().equals("")))
			{
				cond += " and id in (" + esexcerpt.getContent() + ")";
			}else{
				cond += " and id in (0)";
			}
		}
		
		if (q != null){
			cond += " and (address like '%"+q+"%' or mode like '%"+q+"%' ) ";			
		}
		
		/**if (!isAdmin())
            cond += " and z_id = '" + user.getDepartment().getCompany_id() +"'";
        **/ 
		Object[] args = tmpArgs.toArray();
		
		long total = Estate2.count(Estate2.class, cond, args);
		List<Estate2> items = Estate2.findAll(Estate2.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	
	
}
